<?php
class TicketCate extends AppModel {
	public $actsAs = array('Pagination');
	var $tree = array();
	var $recursive = 1;
	
	public $displayField = 'title_zh';
	
	public $validate = array(
		'sys_website_id' => array(
			'rule' => 'notEmpty',
			'message'  => '分类所属产点不能为空'
		),
		'parent_id' => array(
			'rule' => 'notEmpty',
			'message'  => '父级分类不能为空'
		),
		'title_zh' => array(
				'rule' => 'notEmpty',
				'message'  => '分类名称不能为空'
		),
		'sort' => array(
				'numeric' => array(
						'rule' => array('numeric'),
						//'message' => 'Your custom message here',
						//'allowEmpty' => false,
						//'required' => false,
						//'last' => false, // Stop validation after this rule
						//'on' => 'create', // Limit validation to 'create' or 'update' operations
				),
		),
		'is_open' => array(
				'numeric' => array(
						'rule' => array('numeric'),
						//'message' => 'Your custom message here',
						//'allowEmpty' => false,
						//'required' => false,
						//'last' => false, // Stop validation after this rule
						//'on' => 'create', // Limit validation to 'create' or 'update' operations
				),
		),
		'enable' => array(
				'numeric' => array(
						'rule' => array('numeric'),
						//'message' => 'Your custom message here',
						//'allowEmpty' => false,
						//'required' => false,
						//'last' => false, // Stop validation after this rule
						//'on' => 'create', // Limit validation to 'create' or 'update' operations
				),
		),
	);
	
	public $belongsTo = array(
			'SysWebsite' => array(
					'className' => 'SysWebsite',
					'foreignKey' => 'sys_website_id',
					'conditions' => '',
					'fields' => '',
					'order' => ''
			),
			'ArticleCate' => array(
				'className' => 'ArticleCate',
				'foreignKey' => 'article_cate_id',
				'conditions' => '',
				'fields' => '',
				'order' => ''
			),
			'SysSeo' => array(
				'className' => 'SysSeo',
				'foreignKey' => 'sys_seo_id',
				'conditions' => '',
				'fields' => '',
				'order' => ''
			)
		);

	/**
	 * 分页条件
	 * @Date 2012-11-30
	 * @return Array
	 */
	function pageParam($action){
		return array(
				"colNames"=>array(
						'ID','名称','相关资讯分类','票品数','排序','前台组','启用','操作'
				),
				"colModel"=>array(
						array("name"=>'TicketCate.id',"width"=>'50',"sortable"=>false,"align"=>"center"),
						array("name"=>'TicketCate.name',"width"=>"300","sortable"=>false),
						array("name"=>'TicketCate.article_cate_id',"width"=>"200","sortable"=>false),
						array("name"=>'TicketCate.count_tickets',"width"=>"50","sortable"=>false,"align"=>"center"),
						array("name"=>'TicketCate.sort',"width"=>"50","sortable"=>false,"align"=>"center"),
						array("name"=>'TicketCate.is_front',"width"=>"50","sortable"=>false,"align"=>"center"),
						array("name"=>'TicketCate.enable',"width"=>"50","sortable"=>false,"align"=>"center","formatter"=>"checkbox"),
						array("name"=>'act',"width"=>"200","align"=>"center","sortable"=>false)
				),
				"ExpandColumn"=>"TicketCate.name",
				"ajax"=>true,
				"action"=>$action
		);
	}

	/**
	 * 获取票品分类
	 * ext cloum
	 * level/parent/isLeaf/expanded/loaded/icon
	 * @Date 2012-11-30
	 * @param $parentId 上级id
	 * @param $level 层级
	 * @return 
	 */
	function ajaxTreeCategory($parentId,$level){
	
		$t = $this->findCategory($parentId,'all');
		$leafTree = array();
		$row = array();
	
		foreach($t as $k => $v){
			array_push($leafTree,$v['TicketCate']['id']);
			array_push($leafTree,$v['TicketCate']['title_zh'].(($v['TicketCate']['title_en'])?"  ({$v['TicketCate']['title_en']})":''));
			array_push($leafTree,$v['ArticleCate']['title_zh']);
			array_push($leafTree,$v['TicketCate']['count_tickets']);
			array_push($leafTree,$v['TicketCate']['sort']);
			array_push($leafTree,$v['TicketCate']['is_front']);
			array_push($leafTree,$v['TicketCate']['enable']);
			array_push($leafTree,"");
				
			//level
			array_push($leafTree,$level);
			
			//parent
			array_push($leafTree,$parentId);
			
			//isLeaf
			$hasSon = $this->findSonNodel($v['TicketCate']['id'],'all');
			if($hasSon){
				array_push($leafTree,false);
			}else{
				array_push($leafTree,true);
			}
			
			//expanded
			if($v['TicketCate']['is_open']==1){
				$treeLoaded = true;
				array_push($leafTree,true);
			}else{
				$treeLoaded = false;
				array_push($leafTree,false);
			}
			
			//loaded
			array_push($leafTree,$treeLoaded);
				
			$row['id'] = $v['TicketCate']['id'];
			$row['cell'] = $leafTree;
			
			$leafTree = array();
			
			if(sizeof($row)>0){
				array_push($this->tree,$row);
			}
				
			if($v['TicketCate']['is_open']==1){
				$this->ajaxTreeCategory($v['TicketCate']['id'],$level+1);
			}
		}
		return $this->tree;
	}
	
	/**
	 * 获取票品分类分类
	 * @author mingxing.zhao@qq.com  
	 * @Date 2012-12-3
	 * @param
	 * @return 
	 */
	function findCategory($id = 0,$type = 'enable') {
		$sys_website_id = CakeSession::read('SITEID');
		$conditions = array('conditions' => array(
				'TicketCate.parent_id'=>$id,
				'TicketCate.sys_website_id'=>$sys_website_id
			),
				'order' => array('TicketCate.sort ASC')
		) ;
		if($type == 'enable'){
			$conditions['conditions']['TicketCate.enable'] = 1;
		}
		return $this->find('all',$conditions);
	}
	
	/**
	 * 检测子节点
	 * @author mingxing.zhao@qq.com
	 * @Date 2012-12-2
	 * @param
	 * @return
	 */
	public function findSonNodel($parent_id = 0,$type = 'enable'){
		$conditions = array(
				'conditions'=>array(
						'TicketCate.parent_id'=>$parent_id
				)
		);
		if($type == 'enable'){
			$conditions['conditions']['TicketCate.enable'] = 1;
		}
		return $this->find('first',$conditions);
		
	}
	
	/**
	 * 取得票品分类下级列表
	 * @author mingxing.zhao@qq.com  
	 * @Date 2012-12-4
	 * @param
	 * @return 
	 */
	public function findTicketCateList($parent_id = 1){
		$conditions = array(
			'conditions'=>array(
				'parent_id'=>$parent_id,
				'enable'=>1
			),
			'order'=>'sort DESC,id ASC'
		);
		return $this->find('list',$conditions);
	}
	
	public function getSiteEnable($sys_website_id){
		return $this->find('list',array(
				'conditions'=>array(
					'TicketCate.is_front >'=>0,
					'TicketCate.enable'=>1,
					'TicketCate.sys_website_id'=>$sys_website_id		
				),
				'order'=>'TicketCate.is_front ASC'
			));
	}
	
	public function getSiteREnable($sys_website_id){
		return $this->find('list',array(
				'conditions'=>array(
						'TicketCate.parent_id'=>1,
						'TicketCate.enable'=>1,
						'TicketCate.sys_website_id'=>$sys_website_id
				),
				'order'=>'TicketCate.is_front ASC'
		));
	}
}
?>